package com.mcx.modules.proto;

import com.mcx.modules.common.IProto;
import com.mcx.modules.common.MMO_DataStream;

import java.io.*;
import java.util.*;

/**
 * 服务器返回配置列表
 */
public class System_GameServerConfigReturnProto implements IProto
{
    private static MMO_DataStream dataStream = new MMO_DataStream();

    @Override
    public short getProtoCode() { return 14003; }

    public Integer ConfigCount; //配置数量
    public ArrayList<ConfigItem> ServerConfigList; //配置项

    /**
     * 配置项
     */
    public static class ConfigItem
    {
        public String ConfigCode; //配置编码
        public Boolean IsOpen; //是否开启
        public String Param; //参数
    }

    public byte[] ToArray() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputStream outputStream = new DataOutputStream(out)) {
            dataStream.outputStream = outputStream;
            dataStream.WriteShort(getProtoCode());
            dataStream.WriteInt(ConfigCount);
            for (int i = 0; i < ConfigCount; i++) {
                dataStream.WriteUTF8String(ServerConfigList.get(i).ConfigCode);
                dataStream.WriteBool(ServerConfigList.get(i).IsOpen);
                dataStream.WriteUTF8String(ServerConfigList.get(i).Param);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();    }
    public static System_GameServerConfigReturnProto GetProto(byte[] buffer) {
        System_GameServerConfigReturnProto proto = new System_GameServerConfigReturnProto();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            dataStream.inputStream = inputStream;
            proto.ConfigCount = dataStream.ReadInt();
            proto.ServerConfigList = new ArrayList<ConfigItem>();
            for (int i = 0; i < proto.ConfigCount; i++) {
                ConfigItem _ServerConfig = new ConfigItem();
                _ServerConfig.ConfigCode = dataStream.ReadUTF8String();
                _ServerConfig.IsOpen = dataStream.ReadBool();
                _ServerConfig.Param = dataStream.ReadUTF8String();
                proto.ServerConfigList.add(_ServerConfig);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return proto;
    }
}